package dao;

import com.db.DBConn;
import model.Course;
import model.Score;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Vector;

public class ScoreDao {
    public Collection getDatas() throws SQLException {
        DBConn db = new DBConn();
        ResultSet rs = db.queryData("select * from score order by id");
        Collection col = new Vector();
        while (rs.next()) {
            Score score = new Score();
            score.setId(rs.getString("id"));
            score.setSno(rs.getString("sno"));
            score.setSname(rs.getString("sname"));
            score.setCno(rs.getString("cno"));
            score.setCname(rs.getString("cname"));
            score.setScore(rs.getFloat("score"));
            col.add(score);
        }
        return col;
    }

    //提取教师信息by idc
    public Score getData(String id) throws SQLException {
        DBConn db = new DBConn();
        ResultSet rs = db.queryData("select * from score where id="+id);
        if(rs.next()) {
            Score score = new Score();
            score.setId(id);
            score.setSno(rs.getString("sno"));
            score.setSname(rs.getString("sname"));
            score.setCno(rs.getString("cno"));
            score.setCname(rs.getString("cname"));
            score.setScore(rs.getFloat("score"));
            return score;
        }else {
            return null;
        }
    }

    //修改数据
    public void updateData(Score score) throws SQLException {
        DBConn db = new DBConn();
        //String ano = score.getAno();
        db.updateData("update score set sno='"+score.getSno()+"',sname='"+score.getSname()+"',cno='"+score.getCno()+"',cname='"+score.getCname()+"',score='"+score.getScore()+"' where id="+score.getId());
    }

    //新增数据
    public void insertData(Score score) throws SQLException {
        DBConn db = new DBConn();
        db.updateData("insert into score(sno,sname,cno,cname,score) values('"+score.getSno()+"','"+score.getSname()+"','"+score.getCno()+"','"+ score.getCname()+"','"+ score.getScore()+"')");
    }

    //删除数据
    public void deleData(String id) throws SQLException {
        DBConn db = new DBConn();
        db.updateData("delete from score where id="+id);

    }

    public boolean getDataByOne(String sno,String cname){
        DBConn db = new DBConn();
        try{
            ResultSet rs = db.queryData("select * from score where sno='"+sno+"'and cname = '"+cname+"'");
            if(rs.next())
                return true;
            else
                return false;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


        public Collection getDatas(String sno) throws SQLException {
            DBConn db = new DBConn();
            ResultSet rs = db.queryData("select * from score where sno='"+sno+"'");
            Collection col = new Vector();
            while (rs.next()) {
                Score score = new Score();
                score.setId(rs.getString("id"));
                score.setSno(rs.getString("sno"));
                score.setSname(rs.getString("sname"));
                score.setCno(rs.getString("cno"));
                score.setCname(rs.getString("cname"));
                score.setScore(rs.getFloat("score"));
                col.add(score);
            }
            return col;
        }
}
